package com.liwc.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liwc.constant.StatusCode;
import com.liwc.dao.NotepadMapper;
import com.liwc.message.ResponseMessage;
import com.liwc.model.Notepad;
import com.liwc.service.NotepadService;
import com.liwc.util.AESUtil;


@Service
public class NotepadServiceImpl implements NotepadService{
	
	
	// log
	private static final Logger log = LoggerFactory.getLogger(NotepadServiceImpl.class);
		
	@Autowired
	private NotepadMapper dao;

	
	
	@Override
	public Notepad findById(Integer id) {
		if(null == id) 
			return null;
		Notepad np = dao.selectByPrimaryKey(id);
		if(null == np) 
			return null;
		np.setNote(AESUtil.decrypt(np.getNote()));
		np.setNote1(AESUtil.decrypt(np.getNote1()));
		return np;
	}

	@Override
	public PageInfo<Notepad> findAll(Notepad model) {
		
		PageHelper.startPage(model.getPage(), model.getRows());
		List<Notepad> list = dao.selectAll();
		for(Notepad np : list){
			np.setNote(AESUtil.decrypt(np.getNote()));
			np.setNote1(AESUtil.decrypt(np.getNote1()));
		}
		PageInfo<Notepad> pi = new PageInfo<Notepad>(list);
		return pi;
	}

	@Override
	public List<Notepad> getByExample(Notepad model) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResponseMessage save(Notepad model) {
		model.setNote(AESUtil.encrypt(model.getNote()));
		model.setNote1(AESUtil.encrypt(model.getNote1()));
		if(null != model.getId() ){
			dao.updateByPrimaryKeySelective(model);
		}else{
			dao.insertSelective(model);
		}
		return new ResponseMessage(StatusCode.OK, "保存成功", model);
	}

	@Override
	public ResponseMessage deleteById(Integer id) {
		dao.deleteByPrimaryKey(id);
		return new ResponseMessage(StatusCode.OK, "删除成功", id);
	}
	
	
	
	

	
	
	
}
